package com.ljm.boot.helloword.letCode;

public class Test42_no {


    public static int trap(int[] height) {
        int length = height.length-1;
        int left =0;
        if(height[0]<=0){
            left++;
        }
        int right=left+1;
        int maxArea = 0;

        while(left<length||right<length){
            int area2 = 0, ab=left;

            while(height[left]>height[right]){
                if(!getBig(height,height[left],right)){
                    left++;
                    ab++;
                }else{
                    area2=area2+height[ab++];
                }
                if(right==length){
                   break;
                }
                right++;
            }

            area2=area2+height[ab];
            maxArea = Math.min(height[left],height[right])*(right-left)-area2;
            System.out.println("left:"+left+"right:"+right);
            System.out.println("左高:"+height[left]+"右高:"+height[right]+"区域："+maxArea);

            left = right;
            right++;
        }

        return maxArea;
    }



    public static boolean getBig(int[] height, int num ,int index){
        if(index>=height.length-1){
            return false;
        }
        for (int i= index;i<height.length;i++){
            if(height[i]>=num){
                return true;
            }
        }
        return false;
    }

    public static int trap2(int[] height) {
        int sum = 0;
        //最两端的列不用考虑，因为一定不会有水。所以下标从 1 到 length - 2
        for (int i = 1; i < height.length - 1; i++) {
            int max_left = 0;
            //找出左边最高
            for (int j = i - 1; j >= 0; j--) {
                if (height[j] > max_left) {
                    max_left = height[j];
                }
            }
            int max_right = 0;
            //找出右边最高
            for (int j = i + 1; j < height.length; j++) {
                if (height[j] > max_right) {
                    max_right = height[j];
                }
            }
            //找出两端较小的
            int min = Math.min(max_left, max_right);
            //只有较小的一段大于当前列的高度才会有水，其他情况不会有水
            if (min > height[i]) {
                sum = sum + (min - height[i]);
            }
        }
        return sum;
    }



    public static void main(String[] args) {
        int[] height =  new int[]{0,1,0,2,1,0,1,3,2,1,2,1};

        System.out.println(getNum(height));
    }

    public static int getNum(int[] height){
        int sum = 0;
        for(int i=1;i< height.length-1;i++){
            int max_left =0;
            for(int j=i-1;j>=0;j--){
                if(max_left<height[j]){
                    max_left=height[j];
                }
            }
            int maxRight = 0;
            if(max_left==0){continue; }
            for(int k=i+1;k<=height.length-1;k++){
                if(maxRight<height[k]){
                    maxRight=height[k];
                }
            }
            int avg = Math.min(max_left,maxRight);
            if(avg>height[i]){
                sum =sum+(avg-height[i]);
            }
        }
        return sum;
    }

}
